package com.kpijavatraining.module.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactoryConfigurationError;
import org.xml.sax.SAXException;
import com.kpijavatraining.module.io.ConsoleReader;
import com.kpijavatraining.module.io.FileWriter;

public class Registrator {
	
	private ConsoleReader reader;
	private String login;
	private String password;
	private Checker checker;
	private FileWriter writer;
	
	public Registrator() throws ParserConfigurationException, IOException, SAXException{
		checker  = new Checker(); 
		reader = new ConsoleReader();
		writer = new FileWriter();
	}
	
	public boolean register() throws TransformerFactoryConfigurationError, TransformerException, FileNotFoundException{	
		while(true){
			System.out.print("Enter login:");
			login = reader.readLn();
			if(login.equals("exit")){
				return false;
			}
			if(checker.identification(login)){
				System.out.println("користувач з таким іменем вже існує");
				System.out.println("спробуйте інший логін або введіть exit для виходу");
			}else{
				System.out.print("Enter password:");
				password = reader.readLn();
				break;
			}
		}
		writer.write(login,password,"user");
		return true;
	}
}